﻿package kit4j.generator.keyed;

import java.awt.Point;
import java.util.Random;

import kit4j.common.lang.IntegerBound;

/**
 * 随机的指定点值的相邻点值生成器。
 * 
 * @author MiniKnife
 * 
 */
public class RandomAdjacentPointKeyedGenerator implements
		IKeyedGenerator<Point, Point> {
	
	private Random rand;
	
	private IntegerBound bound;
	
	private RandomIntegerKeyedGenerator intGenerator;
	
	/**
	 * 创建一个随机相邻点生成器。
	 */
	public RandomAdjacentPointKeyedGenerator() {
		rand = new Random(System.currentTimeMillis());
		bound = new IntegerBound(-1, 1);
		intGenerator = new RandomIntegerKeyedGenerator();
	}

	/**
	 * 生成下一个与指定点相邻的随机方向的点。
	 * 
	 * @param origin
	 *            原点
	 * @return 与指定点相邻的随机方向的点
	 * @see kit4j.generator.keyed.IKeyedGenerator#next(java.lang.Object)
	 */
	@Override
	public Point next(Point origin) {
		int x = origin.x + intGenerator.next(bound);
		int y = origin.y;
		
		if(x != origin.x) {
			y += intGenerator.next(bound);
		} else {
			if(rand.nextBoolean()) {
				++y;
			} else {
				--y;
			}
		}

		return new Point(x, y);
	}

}
